/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tubes5progin.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author thoriq_r9
 */
public class SqlAccess {

    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet res = null;

    private static String dbname = "filesharing";
    private static String user = "root";
    private static String pass = "";

    private static void connect() throws InstantiationException, IllegalAccessException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost/"+dbname+"?user="+user+"&password="+pass);
            stmt = conn.createStatement();
        }
        catch(ClassNotFoundException ex) {

        }
        catch(SQLException ex) {
        }
    }

    private static void connect2(String server) throws InstantiationException, IllegalAccessException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://"+server+"/"+dbname+"?user="+user+"&password="+pass);
            stmt = conn.createStatement();
        }
        catch(ClassNotFoundException ex) {

        }
        catch(SQLException ex) {
        }
    }

    public static void disconnect() {
        try {
            if(conn!=null) {
                conn.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(SqlAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static int executeUpdate(String query) throws Exception {
        connect();
        return stmt.executeUpdate(query);
    }

    public static void execute(String query) throws Exception {
        connect();
        res = stmt.executeQuery(query);
    }

    public static void execute2(String query, String server) throws Exception {
        connect2(server);
        res = stmt.executeQuery(query);
    }
    
    public static void closeConnection() {
        if(stmt!=null) {
            try {
                stmt.close();
            }
            catch (SQLException ex) {
                Logger.getLogger(SqlAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
            stmt = null;
        }

        if(conn!=null) {
            try {
                conn.close();
            }
            catch (SQLException ex) {
                Logger.getLogger(SqlAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
            conn = null;
        }

        if(res!=null) {
            try {
                res.close();
            }
            catch (SQLException ex) {
                Logger.getLogger(SqlAccess.class.getName()).log(Level.SEVERE, null, ex);
            }
            res = null;
        }
    }

    public static ResultSet getResult() {
        return res;
    }
}
